# Copyright (c) 2021 Contributors to the Eclipse Foundation
# 
# See the NOTICE file(s) distributed with this work for additional
# information regarding copyright ownership.
# 
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# http://www.eclipse.org/legal/epl-2.0
# 
# SPDX-License-Identifier: EPL-2.0
#
import os
import re
import datetime
import xml.etree.ElementTree as ET
import html

import nut_utils

def msgTextNodeToICUString(xmlnode):
    '''The node can contain innertext and tags like <q />'''
    strwithtags = ET.tostring(xmlnode).decode()
    
    #Replace <q />'s with double-quotes
    str1      = re.sub("<\s*q[\s/]*>",'\\"',strwithtags)
    #Replace line breaks with spaces
    str2      = re.sub("\n",' ',str1)
    #Replace <br /> tags
    str3      = re.sub("<\s*br[\s/]*>",'\\\\n',str2)
    #Remove starting tag
    str4  = re.sub("<\s*MsgText[^>]*>",'',str3)
    #Remove ending tag
    str5  = re.sub("<\s*/\s*MsgText[^>]*>",'',str4)
    
    #Replace xml angled brackets etc for the real thing
    str6 = html.unescape(str5)
     
    #Duplicate single quotes
    str7 = re.sub("'","''", str6) 
    
    #Surprisingly (to me) - replace any number of spaces with a single space
    str8 = re.sub("\s{2,}", ' ', str7)
    
    return str8.strip()

def outputICUFile(logger, inputfile, inputxmlroot, outpath, catalogname):
    timestr = datetime.datetime.now().strftime("%H:%M:%S on %d %B %Y")
    logger.info("Converting "+inputfile+" to ICU output file: "+outpath+" at "+timestr)
    
    try:
       outfile = open(outpath, "w", encoding="utf8")
       outfile.write("//ICU output file generated by nut_translation.py\n")
       outfile.write("//Generated from "+inputfile+" at "+timestr+"\n//\n\n")
       outfile.write(catalogname+"{\n")
       for child in inputxmlroot:
           if child.tag == "Message":
               msgnode = child
               msgid = None
               msgtext = None

               if msgnode.attrib['ID']:
                   msgid = msgnode.attrib['ID']

               for mchild in msgnode:
                   if mchild.tag == "MsgText":
                       msgtextnode = mchild
                       msgtext = msgTextNodeToICUString(msgtextnode)
               
               if msgtext and msgid:
                   outfile.write("  "+msgid+' {"'+msgtext+'"}\n')
               else:
                   logger.error("Malformed message")
                   if msgid:
                       logger.error("Maformed message ID is "+msgid)
                   else:
                       logger.error("Maformed message ID is MISSING")
                   if msgtext:
                       logger.error("Maformed message text is "+msgtext)
                   else:
                       logger.error("Maformed message text is MISSING")
       outfile.write("}\n")
       outfile.close()

    except Exception as e:
       logger.error("Failed to write ICU txt file: %s - error %s" %
                         (outpath,  e))
       raise e


def processICU(logger, infile, outfile, catalogname):
    nut_utils.createOutputDir(outfile)
    inputxmlroot = nut_utils.parseFile(infile)
    outputICUFile(logger, infile, inputxmlroot, outfile, catalogname)

def outputICU(logger, langliststr, replace_filename_vars, inputfile, output, catalogstring):
    if langliststr is not None and langliststr != "":
        langlist = nut_utils.parseLanguageLists(langliststr)

        for lang in langlist:
            for infile in inputfile:
                if replace_filename_vars:
                    infile = nut_utils.parseFileNameForLangVars(infile, lang)
                    outfile = nut_utils.parseFileNameForLangVars(output, lang)
                    catalogname  = nut_utils.parseFileNameForLangVars(catalogstring, lang)
                else:
                    outfile = output
                    catalogname = catalogstring

                processICU(logger, infile, outfile, catalogname)
    else:
        for infile in inputfile:
            processICU(logger, infile, output, catalogstring)

